package gemini.pollux.ui.client.page.content.marketingplan.level.edit;

import gemini.basic.model.AbstractDomain;
import gemini.basic.model.Level;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class LevelEditView extends Composite implements
		LevelEditPresenter.Display {

	@UiTemplate("LevelEditView.ui.xml")
	interface MyUiBinder extends UiBinder<Panel, LevelEditView> {
	}

	private static final MyUiBinder binder = GWT.create(MyUiBinder.class);

	public LevelEditView() {
		initWidget(binder.createAndBindUi(this));
	}

	@UiField
	HTML htmTitle;

	@UiField
	HTML htmInformation;

	@UiField
	TextBox tbxName;

	@UiField
	TextBox tbxDiscountScale;

	@UiField
	TextArea tarDescription;

	@UiField
	ListBox lbxNextLevel;

	@UiField
	Button btnSubmit;

	@UiField
	Button btnBack;

	@UiField
	Button btnReset;

	@UiHandler("btnReset")
	void resetFields(ClickEvent event) {
		loadForm();
	}

	@Override
	public void initCustom() {

		tarDescription.setSize("300px", "100px");
		tbxName.setWidth("300px");
		tbxDiscountScale.setWidth("300px");
		lbxNextLevel.setWidth("300px");
		
		tbxName.setMaxLength(30);
		tbxDiscountScale.setMaxLength(2);

		if (level == null) {
			htmTitle.setHTML("<h3>New Level</h3>");
			htmInformation
					.setHTML("Fill bellow information to create new level<br />You should create the biggest level first!");
		} else {
			htmTitle.setHTML("<h3>Update Level</h3>");
			htmInformation.setHTML("Update bellow information to update level");
		}

		loadForm();
	}

	@Override
	public Widget asWidget() {
		return this;
	}

	private Level level;

	private List<Level> levels;

	@Override
	public HasClickHandlers getBackClickHandlers() {
		return btnBack;
	}

	@Override
	public HasClickHandlers getSubmitClickHandlers() {
		return btnSubmit;
	}

	@Override
	public void setLevel(Level level) {
		this.level = level;
	}

	public void loadForm() {
		if (this.level == null) {
			tbxName.setValue("");
			tbxDiscountScale.setValue("");
			tarDescription.setValue("");
		} else {
			tbxName.setValue(this.level.getName());
			tbxDiscountScale.setValue(String.valueOf(this.level
					.getDiscountScale()));
			tarDescription.setValue(this.level.getDescription());
		}

		lbxNextLevel.clear();
		if (levels != null) {
			for (Level lv : levels) {
				if (level == null || !lv.getId().equals(level.getId())) {
					lbxNextLevel.addItem(lv.getName(),
							String.valueOf(lv.getId()));
				}
			}
		}
	}

	@Override
	public Level getLevel() {
		if (level == null) {
			level = new Level();
			level.setId(AbstractDomain.TRANSIENT_ENTITY_ID);
		}
		
		String desc = tarDescription.getValue();

		if(desc != null && desc.length() > 100){
			desc.substring(0, 99);
		}
		
		level.setDescription(desc);
		level.setName(tbxName.getValue());
		level.setDiscountScale(Short.valueOf(tbxDiscountScale.getValue()));

		if (lbxNextLevel.getItemCount() > 0) {
			String nextLevelId = lbxNextLevel.getValue(lbxNextLevel
					.getSelectedIndex());

			if (nextLevelId != null) {
				Level nextLevel = new Level();
				nextLevel.setId(Integer.parseInt(nextLevelId));
				level.setLevel(nextLevel);
			}
		}

		return level;
	}

	@Override
	public void setLevels(List<Level> levels) {
		this.levels = levels;
	}
}
